'use strict';
(function() {
	
	function initParams($scope, $state, Dialog, service){
		$scope.ip = $state.params.ip;
		$scope.port = $state.params.port;
		$scope.monitors = null;
		$scope.newMonitor = {"monitor" : ""};
		
		$scope.showLogs = function(monitor){
			$state.go("main.ma.log", {ip:$scope.ip, port:$scope.port, monitor:monitor.monitor});
		};
		
		$scope.delMonitor = function($event, monitor){
			if(!$scope.ip || !$scope.port){
				return;
			}
			Dialog.confirm("此操作将删除该监控对象下所有的监控日志文件，是否确认删除？", function(){
				service.del($scope.ip, $scope.port, monitor.monitor, function(rst){
					init($scope, $state, Dialog, service);
				});
			});
			$event.stopPropagation();
		};
		
		$scope.addMonitor = function(){
			if(!$scope.ip || !$scope.port){
				return;
			}
			$scope.newMonitor.monitor = $scope.newMonitor.monitor || "anonymous"; 
			
			for(var i = 0; i < $scope.monitors.length; i++){
				var monitor = $scope.monitors[i];
				if(monitor.monitor == $scope.newMonitor.monitor){
					Dialog.show("监控对象[" + $scope.newMonitor.monitor + "]已存在！", 300);
					return;
				}
			}
			service.create($scope.ip, $scope.port, $scope.newMonitor.monitor, function(rst){
				init($scope, $state, Dialog, service);
			});
//			}else {
//				Dialog.show("信息不完整，请完善！", 300);
//			}
		};
	}
	
	function init($scope, $state, Dialog, service){
		if($scope.ip && $scope.port){
			service.list($scope.ip, $scope.port, function(rst){
				$scope.monitors = rst.data;
			});
		}else {
			Dialog.show("请求参数不正确！");
		}
	}
	
    ngApp.controller("mamonitorController", ["$scope", "$state", "Dialog", 'AppConfigConstant', "mamonitorService", function($scope, $state, Dialog, AppConfigConstant, mamonitorService) {
    	initParams($scope, $state, Dialog, mamonitorService);
    	init($scope, $state, Dialog, mamonitorService);
    }]);

})();
